//日历
angular.module('starter.calendar', [])
.directive('calendar',['$ionicPopover','$timeout',function($ionicPopover,$timeout){
    return{
        restrict:'A',
        scope:{
            calendartimedate : '=calendarTime'
        },
        template:['<div ng-click="openPopover($event)">{{miao | date: "yyyy  年  MM  月  dd    日"}}</div>'].join(''),
        link:function(scope,element,attr){
            scope.lastMonth=[];  //上一个月
            scope.nextMonth=[];  //下一个月
            scope.thisMonth=[];  //本月;

            // scope.styleColor='#40BD9D';
            scope.month=function(thisYear,thisMouth){  //算出来一个月有多少天
                var year = thisYear;
                var mouth = thisMouth;
                var days ;

           //当月份为二月时，根据闰年还是非闰年判断天数
                if(mouth == 2){
                    days= year % 4 == 0 ? 29 : 28;
                }else if(mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12){
                    days= 31;
                }else{
        //其他月份，天数为：30.
                    days= 30;
                }
               return days;
            }


            scope.calendarTimeMethods=function(years,month){//日历
            //先要获取本月有多少天   年  月
                var res=scope.month(years, month);
                for(var i=1;i<=res;i++){  //得到这个月
                    scope.thisMonth.push(i);
                }
                //得到1号是星期几
                //new Date('2016/1/25').getDay();
                scope.day=new Date(years+'/'+month+'/1').getDay();//1号是星期几

               
                if(month==1){//当月份为1月点时候
                    var lastMonthB=12;
                    var yearsB=years-1;
                }else{
                    var lastMonthB=month-1;
                    var yearsB=years;
                }
                scope.lastDay=scope.month(yearsB,lastMonthB);//得到上个月天数

                for(var i=0;i<scope.day;i++){//得到上个月现实的天数
                    scope.lastMonth.push(scope.lastDay);
                    scope.lastDay--;
                }
                 scope.lastMonth=scope.lastMonth.reverse();
                 //得到这个月最后一天是星期几
                 scope.nextDay=new Date(years+'/'+month+'/'+res).getDay();//1号是星期几
                 for(var i=1;i<=(42-res-scope.lastMonth.length);i++){
                    scope.nextMonth.push(i);
                 }
            }

           
            scope.lastYearsMethods=function(){//上一年
                scope.yearsDate--;
                scope.lastMonth=[];  //上一个月
                scope.nextMonth=[];  //下一个月
                scope.thisMonth=[];  //本月;
                 //得到一共有多少天
                 var id=scope.month(scope.yearsDate,scope.monthDate);
                 if(id<scope.selectid+1){
                    scope.selectid=id-1;
                 }
                scope.calendarTimeMethods(scope.yearsDate,scope.monthDate);
            }
            scope.nextYearsMethods=function(){//下一年
                scope.yearsDate++;
                scope.lastMonth=[];  //上一个月
                scope.nextMonth=[];  //下一个月
                scope.thisMonth=[];  //本月;
                var id=scope.month(scope.yearsDate,scope.monthDate);
                 if(id<scope.selectid+1){
                    scope.selectid=id-1;
                 }
                scope.calendarTimeMethods(scope.yearsDate,scope.monthDate);
            }
            scope.lastMonthMethods=function(){//上一个月
                if(scope.monthDate==1){
                    scope.yearsDate--;
                    scope.monthDate=12;
                }else{
                    scope.monthDate--;
                }
                
                scope.lastMonth=[];  //上一个月
                scope.nextMonth=[];  //下一个月
                scope.thisMonth=[];  //本月;
                var id=scope.month(scope.yearsDate,scope.monthDate);
                 if(id<scope.selectid+1){
                    scope.selectid=id-1;
                 }
                scope.calendarTimeMethods(scope.yearsDate,scope.monthDate);
            }
            scope.nextMonthMethods=function(){//下一个月
                if(scope.monthDate==12){
                    scope.monthDate=1;
                    scope.yearsDate++;
                }else{
                     scope.monthDate++;
                }
                scope.lastMonth=[];  //上一个月
                scope.nextMonth=[];  //下一个月
                scope.thisMonth=[];  //本月;
                var id=scope.month(scope.yearsDate,scope.monthDate);
                 if(id<scope.selectid+1){
                    scope.selectid=id-1;
                 }
                scope.calendarTimeMethods(scope.yearsDate,scope.monthDate);
            }
            scope.aboveMonth=function(lastDay){//当用户点击到了上一个月到时间
                if(scope.monthDate==1){//当  当前这个月为1月
                    scope.monthDate=12;
                    scope.yearsDate--;
                }else{
                    scope.monthDate--;
                }
                scope.selectid=lastDay-1;
                scope.lastMonth=[];  //上一个月
                scope.nextMonth=[];  //下一个月
                scope.thisMonth=[];  //本月;
                scope.calendarTimeMethods(scope.yearsDate,scope.monthDate);
            }
            scope.underMonth=function(nextDay){//当用户点击了下一个月
                if(scope.monthDate==12){//当  当前这个月为 12的时候
                    scope.monthDate=1;
                    scope.yearsDate++;
                }else{
                    scope.monthDate++;
                }
                scope.selectid=nextDay-1;
                scope.lastMonth=[];  //上一个月
                scope.nextMonth=[];  //下一个月
                scope.thisMonth=[];  //本月;
                scope.calendarTimeMethods(scope.yearsDate,scope.monthDate);
            }


            scope.thisClick=function(key){//点击获取样式
                scope.selectid=key;

            }
            scope.queding=function(){//当用户点击确认按钮
                scope.miao=new Date(scope.yearsDate+'/'+scope.monthDate+'/'+(scope.selectid+1)).getTime();
                scope.calendartimedate=scope.miao;
                element.attr('calendar-time', scope.calendartimedate);
                $timeout(function() {
                    scope.$apply();
                });
                scope.popover.remove();
                $ionicPopover.fromTemplateUrl('templates/sign/calendar.html', {
                    scope: scope
                }).then(function(popover) {
                    scope.popover = popover;
                });
            }

            if(!scope.calendartimedate){
               scope.miao=new Date().getTime(); 
               scope.selectid=new Date().getDate()-1;//得到天
               scope.yearsDate = new Date().getFullYear(); //获取年份；
               scope.monthDate = new Date().getMonth()+1; //获取月份
               scope.calendarTimeMethods(scope.yearsDate,scope.monthDate);
            }else{
               scope.miao=scope.calendartimedate;
               scope.selectid=new Date(scope.calendartimedate).getDate()-1;//得到天
               scope.yearsDate = new Date(scope.calendartimedate).getFullYear(); //获取年份；
               scope.monthDate = new Date(scope.calendartimedate).getMonth()+1; //获取月份
               scope.calendarTimeMethods(scope.yearsDate,scope.monthDate);
            }
            


          scope.openPopover = function($event) {
            $ionicPopover.fromTemplateUrl('templates/sign/calendar.html', {
                scope: scope
            }).then(function(popover) {
                scope.popover = popover;
                scope.popover.show($event);
            });
               
          };
          // scope.closePopover = function() {
          //   scope.popover.hide();
          // };
          // // 清除浮动框
           // scope.$on('$destroy', function() {
           //   scope.popover.remove();
           // });
          // // 在隐藏浮动框后执行
          // scope.$on('popover.hidden', function() {
          //   // 执行代码
          // });
          // // 移除浮动框后执行
          // scope.$on('popover.removed', function() {
          //   // 执行代码
          // });
        }
    }
}])